<template>
	<view class="login">
		<uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
		<button @click="getLogin">一键登录</button>
	</view>
</template>
<script>
	export default {
		methods: {
			async getLogin() {
				let [err, suc] = await uni.getUserProfile({
					desc: '登录授权'
				})
				if (err == "getUserProfile:fail auth deny") return uni.$showMsg('你取消了登录')
				if (err == null && suc.errMsg == "getUserProfile:ok") {
					this.$store.commit('user/getUserInfo', suc.userInfo)
					this.getCode(suc)
				}
			},
			async getCode(userProfile) {
				let [err, suc] = await uni.login()
				if (err !== null || suc.errMsg !== "login:ok") return uni.$showMsg('登录失败')
				const query = {
					code: suc.code,
					encryptedData: userProfile.encryptedData,
					rawData: userProfile.rawData,
					iv: userProfile.iv,
					signature: userProfile.signature
				}
				const {
					data
				} = await uni.$http.post('/api/public/v1/users/wxlogin', query)
				if (data.meta.status == 400) {
					this.$store.commit('user/getToken', suc.code)
					uni.$showMsg('登陆成功!')
				}
			}
		}
	}
</script>
<style lang="scss">
	.login {
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		button {
			background-color: #c00000;
			width: 80%;
		}
	}
</style>